//
//  AppDelegate.swift
//  eHealth
//
//  Created by QFX on 2023/10/24.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // NSURL
        let url:URL = URL(string:"https://www.xieganchuangke.com/ehealth.txt")!
        // NSURLRequest
        let request:URLRequest = URLRequest(url:url)
        // NSURLSession
        let configuration:URLSessionConfiguration = URLSessionConfiguration.default
        let session:URLSession = URLSession(configuration: configuration)

        // NSURLSessionTask
        let task:URLSessionDataTask = session.dataTask(with: request) { data, respond, error in
            if error == nil{
                do {
                    if let data = data{
                        let string = String(data: data, encoding: .utf8)
                        if string?.count ?? 0 > 0,string == "1"{
                            DispatchQueue.main.async {
                                UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
                            }
                        }
                    }
                }
            }
        }
        // 启动任务
        task.resume()

        
        /// 配置默认数据库
        /// 该数据库主要用于匹配食物的卡路里，数据有三列数据，Food：食物名称，Serving：食物描述，Calorie：卡路里 数据库有541条数据
        if let path = Bundle.main.path(forResource: "eHealth", ofType: "sqlite3"){
            if let db = try? VHLSQLiteDataBase.init(.uri(path)) {
                VHLSQLiteDataBase.shared = db
            }
        }
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

